package solutions.leetcode.hard

import java.util.LinkedList

/**
 * @author lizhidong
 * <a href="https://leetcode.cn/problems/XxZZjK/">LCP 70. 沙地治理</a>
 */
private class Solution70 {
    fun sandyLandManagement(size: Int): Array<IntArray> {
        val rst = LinkedList<IntArray>()
        for (i in 1..size) {
            if (i == 1) {
                rst.add(intArrayOf(1, 1))
                continue
            }

            var j = 1
            val last = i * 2 - 1
            while (j <= last) {
                rst.add(intArrayOf(i, j))
                if (j + 4 >= last) {
                    when ((i + 2) % 4) {
                        0 -> {
                            if (i == size) rst.add(intArrayOf(i, last))
                        }
                        1 -> {
                            rst.add(intArrayOf(i, j + 3))
                            if (i == size) rst.add(intArrayOf(i, last))
                        }
                        2 -> rst.add(intArrayOf(i, j + 2))
                        3 -> rst.add(intArrayOf(i, j + 4))
                    }
                    break
                }
                j += 4
            }
        }
        return rst.toTypedArray()
    }
}

fun main() {
    val rst = Solution70().sandyLandManagement(3)
    println(rst.size)
    rst.toList()
        .forEach { println(it.toList()) }
}